﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script>
        $(function () {

            $("#btnOK").click(function () {

                //获取选中的角色id
                var count = $("input[name='chkRole']:checked").length;
                //alert(count)
                var Ids = "";
                $("input[name='chkRole']:checked").each(function (i, e) {
                    //alert($(e).attr("data-id"));
                    Ids += $(e).attr("data-id") + ",";
                });
                //alert(Ids);
                $.ajax({
                    url: "/Role/Insert",
                    type: "post",
                    data: {"RoleIds":Ids,"UserId":@ViewBag.UserId },
                    success:function (returnData) {
                        console.log(returnData)
                        if(returnData){
                            alert("操作成功！");
                        }else{
                            alert("操作失败！");
                        }
                    },error:function() {
    
                    }
                })


            });

        });
    </script>
</head>
<body>

    <table>
        <thead>
            <tr>
                <td>
                    <input type="checkbox" /></td>
                <td>名称</td>
            </tr>
        </thead>
        <tbody>
            @{
                
                var roleList = new BLL.RoleManager().QueryRoleList();
                var userRoleRelationList = ViewBag.roleList as List<Model.OA_UserRoleRelation>; 
                var IsOk =false; 
                if (roleList.Count != 0)
                {
                    foreach (var item in roleList)
                    { 
                <tr>
                    <td> 
                        @if (userRoleRelationList.Count != 0)
                        { 
                            foreach (var i in userRoleRelationList)
                            {
                                if (item.Id == i.RoleId)
                                {
                                    IsOk = true;
                                    break;
                                }
                                else
                                {
                                    IsOk = false;
                                } 
                            }
                        }else
                        {
                          <input type="checkbox" name="chkRole" data-id="@item.Id" />
                        } 
                        @if(IsOk)
                        {
                          <input type="checkbox" name="chkRole" data-id="@item.Id" checked="checked" />
                        }else
                        {
                        <input type="checkbox" name="chkRole" data-id="@item.Id" />
                        }

                    </td>
                    <td>
                        @item.Name
                    </td>
                </tr>
                    }
                }
                
            }
        </tbody>
    </table>
    <input type="button" name="name" value="确定" id="btnOK" />
</body>
</html>
